package com.ruoyi.project.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.WxOpenConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.wechat.domain.ExtConfig;
import com.ruoyi.project.wechat.domain.ExtConfigExample;
import com.ruoyi.project.wechat.domain.WxOpenMaCodeTemplateCustom;
import com.ruoyi.project.wechat.domain.XcxVersion;
import com.ruoyi.project.wechat.service.ExtConfigService;
import com.ruoyi.project.wechat.service.WxConfigService;
import com.ruoyi.project.wechat.service.WxThirdPartyService;
import com.ruoyi.project.wechat.service.XcxVersionService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.WxOpenMaService;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.bean.ma.WxMaOpenCommitExtInfo;
import me.chanjar.weixin.open.bean.ma.WxMaOpenWindow;
import me.chanjar.weixin.open.bean.result.*;
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 微信小程序第三方接口
 */
@RestController
@RequestMapping("/wechat/temp")
public class WxThirdPartyController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WxThirdPartyController.class);

    @Resource
    private WxOpenService openService;

    @Resource
    private WxThirdPartyService wxThirdPartyService;

    @Resource
    private WxOpenComponentService wxOpenComponentService;

    @Resource
    private WxConfigService wxConfigService;

    @Resource
    private XcxVersionService xcxVersionService;

    @Resource
    private ExtConfigService extConfigService;

    @Resource
    private RedisCache redisCache;



    /**
     * 获取草稿箱内的所有临时代码草稿.
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:temp:list')")
    @GetMapping("/draftList")
    public AjaxResult getTemplateDraftList() throws WxErrorException {
        logger.info("获取草稿箱内的所有临时代码草稿");
        List<WxOpenMaCodeTemplateCustom> list = wxThirdPartyService.getTemplateDraftList();
        return AjaxResult.success(list);
    }


    /**
     * 获取代码模版库中的所有小程序代码模版.
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:temp:list')")
    @GetMapping("/templateList")
    public AjaxResult getTemplateList() throws WxErrorException {
        logger.info("获取代码模版库中的所有小程序代码模版");
        List<WxOpenMaCodeTemplateCustom> list = wxThirdPartyService.getTemplateList();
        return  AjaxResult.success(list);
    }

    /**
     * 将草稿箱的草稿选为小程序代码模版.
     * @param draftId 草稿ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:temp:add')")
    @PostMapping("/addTemp")
    public AjaxResult addToTemplate(@RequestParam(name = "draftId") Long draftId) throws WxErrorException {
        logger.info("将草稿箱的草稿选为小程序代码模版");
        wxThirdPartyService.addToTemplate(draftId);
        return AjaxResult.success();
    }

    /**
     * 删除指定小程序代码模版.
     * @param templateId 模板ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:temp:remove')")
    @DeleteMapping("/deleteTemplate")
    public AjaxResult deleteTemplate(@RequestParam(name = "templateId") Long templateId) throws WxErrorException {
        logger.info("删除指定小程序代码模版");
        wxThirdPartyService.deleteTemplate(templateId);
        return AjaxResult.success();
    }

    /**
     * 上传已经授权小程序代码
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:temp:commit')")
    @PostMapping("/commit")
    public AjaxResult wxCommit(@RequestBody XcxVersion xcxVersion) throws WxErrorException {
        logger.info("上传已经授权小程序代码 appId: "+xcxVersion.getAppId());
        return wxThirdPartyService.wxCommit(xcxVersion);
    }



    /**
     * 绑定小程序体验者
     * @param wechatId 体验者微信号
     * @param appId 指定小程序appid
     * @return
     * @throws WxErrorException
     */
    @PostMapping("/bindTester")
    public AjaxResult bindTester(@RequestParam(name = "appId") String appId, @RequestParam(name = "wechatId") String wechatId) throws WxErrorException {
        logger.info("绑定小程序体验者 appId: "+appId + " wechatId: "+wechatId);
        WxOpenMaService service = wxOpenComponentService.getWxMaServiceByAppid(appId);
        WxOpenMaBindTesterResult wxOpenMaBindTesterResult = service.bindTester(wechatId);
        return AjaxResult.success(wxOpenMaBindTesterResult);
    }

    /**
     * 解除绑定小程序体验者
     * @param wechatId 体验者微信号
     * @return
     * @throws WxErrorException
     */
    @PostMapping("/unbindTester")
    public AjaxResult unbindTester(@RequestParam(name = "appId") String appId, @RequestParam(name = "wechatId") String wechatId) throws WxErrorException {
        logger.info("绑定小程序体验者 appId: "+appId + " wechatId: "+wechatId);
        WxOpenMaService service = wxOpenComponentService.getWxMaServiceByAppid(appId);
        WxOpenResult wxOpenResult = service.unbindTester(wechatId);
        return AjaxResult.success(wxOpenResult);
    }


    /**
     * 获得体验者列表
     */
    @GetMapping("/getTesterList")
    public AjaxResult getTesterList(@RequestParam(name = "appId") String appId) throws WxErrorException {
        logger.info("获得体验者列表 appId: "+appId);
        WxOpenMaService service = wxOpenComponentService.getWxMaServiceByAppid(appId);
        WxOpenMaTesterListResult testerList = service.getTesterList();
        return AjaxResult.success(testerList);
    }







}
